package simpleftp.client.control;

import java.io.File;
import java.util.concurrent.Callable;
import simpleftp.client.gui.FTPClientWindow;

public final class Downloader implements Callable<Boolean>
{
	private SimpleFTP client;
	private String server;
	private String port;
	private FTPClientWindow wind;
	private String user;
	private String pass;
	private File dir;
	private File file;

	public Downloader(File choosenDir, FTPClientWindow window, String server, String port,
			String user, String pass) {
		this.dir = choosenDir;
		this.server = server;
		this.port = port;
		this.wind = window;
		this.user = user;
		this.pass = pass;
		file = new File(window.focusedFile.toString());
		client = new SimpleFTP();
	}
	@Override
	public Boolean call() throws Exception
	{
		System.out.println("Download starting for " + file.getName());
		Boolean result = false;
		client.connect(server, Integer.parseInt(port), user, pass);
		
		result = client.get(dir, file); // file is the directory where downloading the file.
		
		if (result) {
			wind.console.append("Successfully downloaded file "
					+ file.getName());
			System.out.println("Successfully downloaded  file "
					+ file.getName());
			
		} else {
			wind.console.append("Failed downloading the file " + file.getName());
			System.out.println("Failed downloading the file "
					+ file.getName());
		}
		return result;
	}

}
